<template>
  <div>
    <my-breadcrumb :items="['社区管理', '固定活动']"/>
    <search :formItems="searchItems" @search="getList" ref="search"/>
    <div class="add">
      <el-button type="primary" @click="toAdd">修改配置</el-button>
    </div>
    <el-table :data="activities" @sort-change="orderByTime">
      <el-table-column
        type="index"
        width="55">
      </el-table-column>
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        label="创建时间"
        sortable="custom"
        prop="createtime">
      </el-table-column>
      <el-table-column
        label="活动名称"
        prop="title">
      </el-table-column>
      <el-table-column
        label="活动说明"
        :show-overflow-tooltip="true"
        prop="content">
      </el-table-column>
      <el-table-column
        label="开始时间"
        sortable="custom"
        prop="strattime">
      </el-table-column>
      <el-table-column
        label="结束时间"
        sortable="custom"
        prop="endtime">
      </el-table-column>
      <el-table-column
        label="鲜花总数"
        width="110px"
        prop="flowercount">
      </el-table-column>
      <el-table-column
        label="剩余鲜花"
        sortable="custom"
        width="110px"
        prop="flower">
      </el-table-column>
      <el-table-column
        label="活动状态"
        prop="status">
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
    <el-dialog title="配置修改" :visible.sync="showAdd" width="600px">
      <el-form :model="activity" :rules="rules" label-width="90px" ref="addForm">
        <el-form-item label="活动名称" prop="title">
          <el-input v-model="activity.title"/>
        </el-form-item>
        <el-form-item label="活动说明" prop="content">
          <el-input v-model="activity.content" type="textarea" :autosize="{ minRows: 4}"/>
        </el-form-item>
        <el-form-item label="活动周期" prop="type">
          <el-radio-group v-model="activity.type">
            <el-radio label="1">按周</el-radio>
            <el-radio label="2">按月</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="活动时间" prop="start">
          <div v-show="activity.type === '1'">
            <el-select v-model="activity.weekStart">
              <el-option value="1" label="周一"/>
              <el-option value="2" label="周二"/>
              <el-option value="3" label="周三"/>
              <el-option value="4" label="周四"/>
              <el-option value="5" label="周五"/>
              <el-option value="6" label="周六"/>
              <el-option value="7" label="周日"/>
            </el-select>
            至
            <el-select v-model="activity.weekEnd">
              <el-option value="1" label="周一"/>
              <el-option value="2" label="周二"/>
              <el-option value="3" label="周三"/>
              <el-option value="4" label="周四"/>
              <el-option value="5" label="周五"/>
              <el-option value="6" label="周六"/>
              <el-option value="7" label="周日"/>
            </el-select>
          </div>
          <div v-show="activity.type === '2'">
            <el-input-number :min="1" :max="31" v-model="activity.mouthStart"/>
            至
            <el-input-number :min="activity.mouthStart" :max="31" v-model="activity.mouthEnd"/>
          </div>
        </el-form-item>
        <el-form-item label="鲜花数量" prop="flower">
          <el-input-number v-model="activity.flower" :min="0"/>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="setConfig" :loading="addLoading">{{ addLoading ? '处理中...' : '提交' }}</el-button>
        <el-button type="danger" @click="showAdd = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'fixed-activity',
  data () {
    return {
      searchItems: [
        {
          label: '创建时间',
          type: 'times',
          key: 'times'
        },
        {
          label: '标题',
          type: 'input',
          key: 'title'
        }
      ], // 检索结构
      activities: [],
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0
      },
      showAdd: false,
      addLoading: false,
      lifting: null,
      activity: {
        title: '',
        content: '',
        type: '1',
        weekStart: '',
        weekEnd: '',
        monthStart: '',
        monthEnd: '',
        flower: 0
      },
      rules: {
        title: [
          { required: true, message: '请输入活动标题', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入活动内容', trigger: 'blur' }
        ],
        flower: [
          { required: true, message: '请输入鲜花数量', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择类型', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    getList () {
      let searchData = {}
      if (this.$refs['search']) {
        searchData = this.$refs['search'].search()
      }
      let params = {...searchData, page: this.page.pageNum, size: this.page.pageSize, orderby: this.lifting}
      this.axios.get('/Activity/index?isbackstage=1', {params})
        .then(res => {
          if (res.data.status) {
            this.page.total = Number(res.data.data.count)
            this.activities = res.data.data.list
          }
        })
    },
    handleSizeChange (e) {
      this.page.pageSize = e
      this.getList()
    },
    handleCurrentChange (e) {
      this.page.pageNum = e
      this.getList()
    },
    orderByTime (lifting) {
      if (lifting.order === 'ascending') {
        this.lifting = lifting.prop + ' asc'
      } else {
        this.lifting = lifting.prop + ' desc'
      }
      this.getList()
    },

    toAdd () {
      this.showAdd = true
    },
    setConfig () {
      this.$refs['addForm'].validate((valid) => {
        if (valid) {
          let params = {
            title: this.activity.title,
            content: this.activity.content,
            type: this.activity.type,
            flower: this.activity.flower,
            start: this.activity.type === '1' ? this.activity.weekStart : this.activity.mouthStart,
            end: this.activity.type === '1' ? this.activity.weekEnd : this.activity.mouthEnd
          }
          this.axios.post('/Activity/editactivityconfig', this.qs.stringify(params))
            .then(res => {
              this.addLoading = false
              if (res.data.status === 1) {
                this.$message.success(res.data.msg)
                this.getList()
                this.showAdd = false
              }
            })
        } else {
          this.$message.error('请检查表单是否填写正确')
          this.addLoading = false
          return false
        }
      })
    },
    initConfig () {
      this.axios.get('/Activity/getactivityconfig')
        .then(res => {
          if (res.data.status === 1) {
            Object.keys(res.data.data).forEach(key => {
              this.activity[key] = res.data.data[key]
            })
            if (res.data.data.type === '1') {
              this.activity.weekStart = res.data.data.start
              this.activity.weekEnd = res.data.data.end
            } else if (res.data.data.type === '2') {
              this.activity.mouthStart = res.data.data.start
              this.activity.mouthEnd = res.data.data.end
            }
          }
        })
    }
  },
  created () {
    this.getList()
    this.initConfig()
  }
}
</script>

<style scoped>

</style>
